package com.leetcode.partition17;

import java.io.*;
import java.util.*;

/**
 * @author `RKC`
 * @date 2022/3/30 21:56
 */
public class LC1606找到处理最多请求的服务器 {

    private static final int N = 100010;
    private static int[] cnt = new int[N];
    //小顶堆存放<服务器编号, 时间>，TreeSet存放当前空闲服务器
    private static PriorityQueue<int[]> heap = new PriorityQueue<>(N, (o1, o2) -> Integer.compare(o1[1], o2[1]));
    private static TreeSet<Integer> treeSet = new TreeSet<>();
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int k = Integer.parseInt(ss[0]), n = Integer.parseInt(ss[1]);
        int[] arrival = new int[n], load = new int[n];
        ss = reader.readLine().split(" ");
        for (int i = 0; i < n; i++) arrival[i] = Integer.parseInt(ss[i]);
        ss = reader.readLine().split(" ");
        for (int i = 0; i < n; i++) load[i] = Integer.parseInt(ss[i]);
        writer.write(busiestServers(k, arrival, load).toString());
        writer.flush();
    }

    public static List<Integer> busiestServers(int k, int[] arrival, int[] load) {
        n = arrival.length;
        Arrays.fill(cnt, 0);
        heap.clear();
        treeSet.clear();
        int maxc = 0;
        //初始时都是空闲状态
        for (int i = 0; i < k; i++) treeSet.add(i);
        for (int i = 0; i < n; i++) {
            int start = arrival[i], end = start + load[i];
            //找有空闲的服务器放入treeset
            while (!heap.isEmpty() && heap.peek()[1] <= start) treeSet.add(heap.poll()[0]);
            Integer res = treeSet.ceiling(i % k);
            if (res == null) res = treeSet.ceiling(0);
            //从0开始都没有空闲服务器，当前任务就只能丢弃
            if (res == null) continue;
            treeSet.remove(res);
            heap.add(new int[]{res, end});
            maxc = Math.max(maxc, ++cnt[res]);
        }
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            if (cnt[i] == maxc) ans.add(i);
        }
        return ans;
    }
}
